<template>
  <div class="home">
    <el-container>
      <el-header>
        <div class="hender_left">
          <a href="#/" >
            <img src="../assets/ithtime.png" alt="">
            <span>&nbsp;电商后台管理系统</span>
          </a>
        </div>
        <div>
          <el-button type="info" @click="open">退出</el-button>
        </div>
      </el-header>
      <el-container>
        <el-aside :width="collapse?'64px':'200px'">
            <div class="menuBtn" @click="collapse=!collapse">| | |</div>
          <el-menu
            :default-active="defaultActive"
            class="el-menu-vertical-demo"
            background-color="#373d41"
            text-color="#fff"
            active-text-color="#ffd04b"
            :unique-opened="true"
            :collapse='collapse'
            :collapse-transition='false'
            :router="true"
            >
            <el-submenu :index="'/'+obj1.path" v-for="(obj1, index1) in navMenu" :key="index1" >
              <template slot="title">
                <i :class="iconList[obj1.id]"></i>
                <span>&nbsp;{{obj1.authName}}</span>
              </template>
              <el-menu-item-group v-for="(obj2,index2) in obj1.children" :key="index2">
                <el-menu-item :index="'/'+obj2.path"><i class="el-icon-menu"></i>{{obj2.authName}}</el-menu-item>
              </el-menu-item-group>
            </el-submenu>
          </el-menu>
        </el-aside>
        <el-main>
          <router-view></router-view>
        </el-main>
      </el-container>
    </el-container>
  </div>
</template>

<script>
import { navMenuApi } from '@/api'
export default {
  name: 'Home',
  data () {
    return {
      navMenu: [],
      iconList: {
        125: 'iconfont icon-users',
        103: 'iconfont icon-tijikongjian',
        101: 'iconfont icon-3702mima',
        102: 'iconfont icon-danju',
        145: 'iconfont icon-baobiao'
      },
      collapse: false,
      defaultActive: location.hash.slice(2)
    }
  },
  created () {
    navMenuApi().then(res => {
      this.navMenu = res.data.data
    })
  },
  components: {

  },
  methods: {
    open () {
      this.$confirm('确认退出', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        localStorage.removeItem('token')
        this.$router.push('/login')
      }).catch(() => {
        this.$message({
          type: 'info',
          message: '已取消删除'
        })
      })
    }
  },
  // 桥黑板！！！！  这里使用局部的路由守卫
  beforeRouteUpdate (to, from, next) {
    this.defaultActive = to.path.slice(0)
    next()
  }
}
</script>
<style lang="less" scoped>
.home {
  height: 100%;
}
.el-header {
  display:flex;
  background-color: #373d41;
  color: #333;
  text-align: center;
  line-height: 60px;
  justify-content: space-between;
  .hender_left a{
    display: flex;
    text-decoration: none;
    span{
      font-size: 26px;
      color: #fff;
    }
  }
}
.el-aside{
  transition: all 0.2s ease;
}
.menuBtn{
  font-size: 16px;
  color: #FFF;
  background-color: #4a5064;
  line-height: 1;
  padding: 6px 0;
  cursor: pointer;
}
.el-container,.is-vertical {
  height: 100%;
}
.el-aside {
  background-color: #373d41;
  color: #333;
  text-align: center;
  height: 100%;
}

.el-main {
  background-color: #E9EEF3;
  padding:15px;
}
.el-menu{
  border-right:none;
}
.el-submenu .el-menu-item{
  min-width: 0;
}
</style>
